package com.android.launcher3;

import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

/**
 * Represents a launchable application. An application is made of a name (or
 * title), an intent and an icon.
 */
class ApplicationInfo extends ItemInfo {

	public static final String TAG = "ApplicationInfo";

	int editPageNum = -1;
	int editTopNum = -1;
	int editCellNum;
	int pageNum = -1;
	int topNum = -1;
	int cellNum = -1;
	Bitmap iconBgBitmap;
	Bitmap iconBitmap;
	boolean isUpdated;
	Bitmap titleBitmap;
	int badgeCount;

	/** The application name. */
	CharSequence title;

	/** The intent used to start the application. */
	Intent intent;

	/** The application icon. */
	Drawable icon;

	/** When set to true, indicates that the icon has been resized. */
	boolean filtered;

	/**
	 * Indicates whether the icon comes from an application's resource (if
	 * false) or from a custom Bitmap (if true.)
	 */
	boolean customIcon;

	/**
	 * If isShortcut=true and customIcon=false, this contains a reference to the
	 * shortcut icon as an application's resource.
	 */
	Intent.ShortcutIconResource iconResource;

	boolean systemApp;
	ComponentName componentName;

	/**
	 * Creates a new ApplicationInfo object.
	 */
	ApplicationInfo() {
		itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_SHORTCUT;
	}

	/**
	 * Creates a new ApplicationInfo object.
	 * 
	 * @param paramApplicationInfo
	 */
	public ApplicationInfo(ApplicationInfo paramApplicationInfo) {
		super(paramApplicationInfo);

		ComponentName localComponentName = paramApplicationInfo.componentName;
		this.componentName = localComponentName;
		this.title = paramApplicationInfo.title.toString();
		this.intent = paramApplicationInfo.intent;
		this.systemApp = paramApplicationInfo.systemApp;
		this.topNum = paramApplicationInfo.topNum;
		this.pageNum = paramApplicationInfo.pageNum;
		this.cellNum = paramApplicationInfo.cellNum;
		this.isUpdated = paramApplicationInfo.isUpdated;
		this.editTopNum = paramApplicationInfo.editTopNum;
		this.editPageNum = paramApplicationInfo.editPageNum;
		this.editCellNum = paramApplicationInfo.editCellNum;
		this.badgeCount = paramApplicationInfo.badgeCount;
	}

	/**
	 * Creates a new ApplicationInfo object.
	 * 
	 * @param info
	 * @param iconCache
	 */
	public ApplicationInfo(ResolveInfo info, IconCache iconCache) {
		this.componentName = new ComponentName(
				info.activityInfo.applicationInfo.packageName,
				info.activityInfo.name);
		this.container = ItemInfo.NO_ID;
		setActivity(this.componentName, Intent.FLAG_ACTIVITY_NEW_TASK
				| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		iconCache.getTitleAndIcon(this, info);
	}

	/**
	 * Creates the application intent based on a component name and various
	 * launch flags. Sets {@link #itemType} to
	 * {@link LauncherSettings.BaseLauncherColumns#ITEM_TYPE_APPLICATION}.
	 * 
	 * @param className
	 *            the class name of the component representing the intent
	 * @param launchFlags
	 *            the launch flags
	 */
	final void setActivity(ComponentName className, int launchFlags) {
		intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);
		intent.setComponent(className);
		intent.setFlags(launchFlags);
		itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
	}

	@Override
	void onAddToDatabase(ContentValues values) {
		super.onAddToDatabase(values);

		String titleStr = title != null ? title.toString() : null;
		values.put(LauncherSettings.BaseLauncherColumns.TITLE, titleStr);

		String uri = intent != null ? intent.toUri(0) : null;
		values.put(LauncherSettings.BaseLauncherColumns.INTENT, uri);

		if (customIcon) {
			values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
					LauncherSettings.BaseLauncherColumns.ICON_TYPE_BITMAP);
			Bitmap bitmap = ((FastBitmapDrawable) icon).getBitmap();
			writeBitmap(values, bitmap);
		} else {
			values.put(LauncherSettings.BaseLauncherColumns.ICON_TYPE,
					LauncherSettings.BaseLauncherColumns.ICON_TYPE_RESOURCE);
			if (iconResource != null) {
				values.put(LauncherSettings.BaseLauncherColumns.ICON_PACKAGE,
						iconResource.packageName);
				values.put(LauncherSettings.BaseLauncherColumns.ICON_RESOURCE,
						iconResource.resourceName);
			}
		}
	}

	@Override
	public String toString() {
		return "(" + title.toString() + " pageNum " + this.pageNum
				+ " cellNum " + this.cellNum + ")";
	}

	/**
	 * 完整
	 * 
	 * @return
	 */
	public ShortcutInfo makeShortcut() {
		return new ShortcutInfo(this);
	}
}
